using AutoMapper;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ShopPowerTemplate.Entities;
using ShopPowerTemplate.IoC;
using ShopPowerTemplate.Presentation.Mvc.Controllers;
using ShopPowerTemplate.Presentation.Mvc.Extensions.BootStrapper;
using ShopPowerTemplate.Presentation.Mvc.ViewModels;
using ShopPowerTemplate.Services.Tests;
using System.Web.Mvc;

namespace ShopPowerTemplate.Presentation.Mvc.Test
{
    [TestClass]
    public class CategoryControllerTestBase
    {
        [TestInitialize]
        public virtual void TestInitialize()
        {
            IBootStrapper bootStrapper = new DefaultBootStrapper(IoCFactory.Instance.CurrentContainer);
            bootStrapper.Boot();
        }

        [TestMethod]
        public virtual void CategoryController_IndexTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<CategoryController>());

            var result = controller.Index() as ViewResult;
            
            Assert.IsNotNull(result);
            //ViewName is empty when ViewResult matches action name
            Assert.IsTrue(string.IsNullOrEmpty(result.ViewName));
            Assert.IsTrue(result.Model is IndexCategoryViewModel);
        }

        [TestMethod]
        public virtual void CategoryController_LoadTest()
        {
            CategoryServiceUtil.CreateCategory();
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<CategoryController>());

            var result = controller.Load("", "", 0, 10, "") as JsonResult;

            var rows = ModelHelper.GetList(result.Data);
            Assert.IsTrue(rows.Count > 0);
        }

        [TestMethod]
        public virtual void CategoryController_DeleteTest()
        {
            Category category = CategoryServiceUtil.CreateCategory();
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<CategoryController>());

            var result = controller.Delete(category.Id) as ContentResult;

            Assert.IsNotNull(result);
            Assert.IsTrue(string.IsNullOrEmpty(result.Content));
        }

        [TestMethod]
        public virtual void CategoryController_ReportTest()
        {
            Category category = CategoryServiceUtil.CreateCategory();
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<CategoryController>());

            var result = controller.Report() as FileContentResult;

            Assert.IsNotNull(result);
            Assert.IsTrue(result.ContentType == "text/csv");
            Assert.IsNotNull(result.FileContents.Length > 0);
        }

        [TestMethod]
        public virtual void CategoryController_GetCreateTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<CategoryController>());

            var result = controller.Create() as ViewResult;

            Assert.IsNotNull(result);
            //ViewName is empty when ViewResult matches action name
            Assert.IsTrue(string.IsNullOrEmpty(result.ViewName));
            Assert.IsTrue(result.Model is CreateCategoryViewModel);
        }

        [TestMethod]
        public virtual void CategoryController_PostCreateTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<CategoryController>());
            Category category = CategoryServiceUtil.GetCategory();
            CreateCategoryViewModel viewModel = Mapper.Map<Category, CreateCategoryViewModel>(category);

            var result = controller.Create(viewModel) as RedirectToRouteResult;

            Assert.IsNotNull(result);
            Assert.AreEqual(result.RouteValues["action"], "Edit"); //Create redirect to edit action on success
            Assert.IsTrue((int)result.RouteValues["id"] > 0); //Check new item id
        }

        [TestMethod]
        public virtual void CategoryController_GetEditTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<CategoryController>());
            Category category = CategoryServiceUtil.CreateCategory();

            var result = controller.Edit(category.Id) as ViewResult;

            Assert.IsNotNull(result);
            //ViewName is empty when ViewResult matches action name
            Assert.IsTrue(string.IsNullOrEmpty(result.ViewName));
            Assert.IsTrue(result.Model is EditCategoryViewModel);
            Assert.AreEqual((result.Model as EditCategoryViewModel).Name, category.Name);
        }

        [TestMethod]
        public virtual void CategoryController_PostEditTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<CategoryController>());
            Category category = CategoryServiceUtil.CreateCategory();
            category.Name = "Test";

            EditCategoryViewModel viewModel = Mapper.Map<Category, EditCategoryViewModel>(category);
            controller.Edit(viewModel);

            var result = controller.Edit(category.Id) as ViewResult;
            Assert.IsNotNull(result);
            Assert.AreEqual((result.Model as EditCategoryViewModel).Name, "Test");
        }

        [TestMethod]
        public virtual void CategoryController_DetailTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<CategoryController>());
            Category category = CategoryServiceUtil.CreateCategory();

            var result = controller.Details(category.Id) as ViewResult;

            Assert.IsNotNull(result);
            //ViewName is empty when ViewResult matches action name
            Assert.IsTrue(string.IsNullOrEmpty(result.ViewName));
            Assert.IsTrue(result.Model is DetailsCategoryViewModel);
            Assert.AreEqual((result.Model as DetailsCategoryViewModel).Name, category.Name);
        }

		/* Details region test */
        [TestMethod]
        public virtual void CategoryController_LoadSubcategoriesTest()
        {
            Subcategory subcategories = SubcategoryServiceUtil.CreateSubcategoryFull();
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<CategoryController>());

            var result = controller.LoadSubcategories("", "", 0, 10, "", 1, subcategories.Category.Id) as JsonResult;

            var rows = ModelHelper.GetList(result.Data);
            Assert.IsTrue(rows.Count > 0);
        }
        
        [TestMethod]
        public virtual void CategoryController_AddSubcategoriesTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<CategoryController>());
            Subcategory subcategories = SubcategoryServiceUtil.GetSubcategoryFull();
            PopUpSubcategoryViewModel viewModel = Mapper.Map<Subcategory, PopUpSubcategoryViewModel>(subcategories);

            var result = controller.AddSubcategories(viewModel) as ContentResult;

            Assert.IsNotNull(result);
            Assert.AreEqual(result.Content, "OK");
        }

        [TestMethod]
        public virtual void CategoryController_GetEditSubcategoriesTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<CategoryController>());
            Subcategory subcategories = SubcategoryServiceUtil.CreateSubcategoryFull();

            var result = controller.EditSubcategories(subcategories.Id, subcategories.Category.Id) as PartialViewResult;

            Assert.IsNotNull(result);
            //ViewName is empty when ViewResult matches action name
            Assert.AreEqual(result.ViewName, "~/Views/Subcategory/PopUp.cshtml");
            Assert.IsTrue(result.Model is PopUpSubcategoryViewModel);
			Assert.AreEqual((result.Model as PopUpSubcategoryViewModel).Name, subcategories.Name);
            
        }

        [TestMethod]
        public virtual void CategoryController_PostEditSubcategoriesTest()
        {
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<CategoryController>());
            Subcategory subcategories = SubcategoryServiceUtil.CreateSubcategoryFull();
            subcategories.Name = "Test";

            PopUpSubcategoryViewModel viewModel = Mapper.Map<Subcategory, PopUpSubcategoryViewModel>(subcategories);
            controller.EditSubcategories(viewModel);

            var result = controller.EditSubcategories(subcategories.Id) as PartialViewResult;
            Assert.IsNotNull(result);
			Assert.AreEqual((result.Model as PopUpSubcategoryViewModel).Name, "Test");
        }

        [TestMethod]
        public virtual void CategoryController_DeleteSubcategoriesTest()
        {
            Subcategory subcategories = SubcategoryServiceUtil.CreateSubcategoryFull();
            var controller = WebHelper.GetMockedController(IoCFactory.Resolve<CategoryController>());

            controller.DeleteSubcategories(subcategories.Id, subcategories.Category.Id);

            Assert.AreEqual(controller.Response.StatusCode, 200);
        }

    }
}

